package com.uzai.console.service.md.tljdetail.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.User;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.dto.md.tljdetail.TljDetailIdDto;
import com.uzai.console.dto.md.tljdetail.TljDetailQuery;
import com.uzai.console.entity.MdActivity;
import com.uzai.console.entity.TljDetail;
import com.uzai.console.mapper.MdActivityMapper;
import com.uzai.console.mapper.TljDetailMapper;
import com.uzai.console.service.md.tljdetail.TljDetailService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.md.tljdetail.TljDetailVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 淘礼金
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class TljDetailServiceImpl implements TljDetailService {

    Logger logger = LoggerFactory.getLogger(TljDetailServiceImpl.class);

    @Autowired
    private TljDetailMapper tljDetailMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private MdActivityMapper mdActivityMapper;

    /**
     * 查询免单兑换记录列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tljDetailQuery
     * @return null
     */
    public Page<TljDetailVo> findTljDetailList(TljDetailQuery tljDetailQuery){
        //新建返回到页面的LIST对象
        List<TljDetailVo> tljDetailVoList = new ArrayList<>();

        if(tljDetailQuery.getActivityId() != null){
            MdActivity mdActivity = mdActivityMapper.selectByActivityId(tljDetailQuery.getActivityId(), tljDetailQuery.getMerId());
            if(mdActivity != null){
                tljDetailQuery.setActivityId(mdActivity.getId());
            }
        }

        //执行分页查询对象
        Page<TljDetail> page = new Page<>();
        tljDetailQuery.convert(page);
        ArrayList<TljDetail> tljDetailList =  tljDetailMapper.findByList(page, tljDetailQuery);
        if(tljDetailList != null && tljDetailList.size() > 0){
            for (TljDetail tljDetail : tljDetailList){
                TljDetailVo tljDetailVo = new TljDetailVo();
                BeanUtils.copyProperties(tljDetail, tljDetailVo);

                if(tljDetail.getUserId() != null){
                    User userInfo = userService.findUserInfo(tljDetail.getUserId(), null, tljDetail.getMerId());
                    if(userInfo != null){
                        tljDetailVo.setUserName(userInfo.getNickName());
                    }
                }

                //查询活动id
                if(tljDetail.getActivityId() != null){
                    MdActivity mdActivity = mdActivityMapper.selectById(tljDetail.getActivityId(), tljDetail.getMerId());
                    if(mdActivity != null){
                        tljDetailVo.setActivityId(mdActivity.getActivityId());
                        tljDetailVo.setActivityName(mdActivity.getName());
                    }
                }

                tljDetailVoList.add(tljDetailVo);
            }
        }
        //返回到页面分页对象
        Page<TljDetailVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(tljDetailVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询免单兑换记录详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tljDetailIdDto
     * @return null
     */
    public TljDetailVo findTljDetail(TljDetailIdDto tljDetailIdDto){
        if(tljDetailIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        TljDetail tljDetail = tljDetailMapper.selectById(tljDetailIdDto.getId(), tljDetailIdDto.getMerId());

        if(tljDetail == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该免单兑换记录不存在");
        }

        TljDetailVo tljDetailVo = new TljDetailVo();
        BeanUtils.copyProperties(tljDetail, tljDetailVo);

        if(tljDetail.getUserId() != null){
            User userInfo = userService.findUserInfo(tljDetail.getUserId(), null, tljDetail.getMerId());
            if(userInfo != null){
                tljDetailVo.setUserName(userInfo.getNickName());
            }
        }

        //查询活动id
        if(tljDetail.getActivityId() != null){
            MdActivity mdActivity = mdActivityMapper.selectById(tljDetail.getActivityId(), tljDetail.getMerId());
            if(mdActivity != null){
                tljDetailVo.setActivityId(mdActivity.getActivityId());
                tljDetailVo.setActivityName(mdActivity.getName());
            }
        }
        return tljDetailVo;
    }

    /**
     * 删除淘礼金
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tljDetailIdDto
     * @return null
     */
    public void deleteTljDetail(TljDetailIdDto tljDetailIdDto){
        if(tljDetailIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        tljDetailMapper.deleteById(tljDetailIdDto.getId(), tljDetailIdDto.getMerId());
    }


}
